﻿#include "lix/failable.h"

// 妥協、本来は strerror とか使っちゃいけない。
#pragma warning ( disable : 4996 )


std::wstring lix::Win32ErrorCategory::getTextW( DWORD code)
{
	if(code == ERROR_SUCCESS ){
		return L"";
	}

	LPVOID lpMsgBuf;

	BOOL ok = FormatMessage(
		FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		code,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPTSTR) &lpMsgBuf,
		0,
		NULL
		);
	if( lpMsgBuf == NULL ){
		return L"";
	}
	std::wstring ret = (const wchar_t*)lpMsgBuf;
	LocalFree(lpMsgBuf);
	return ret;
}

std::string lix::Win32ErrorCategory::getText( code_type code)
{
	if(code == ERROR_SUCCESS ){
		return "";
	}

	LPVOID lpMsgBuf;

	BOOL ok = FormatMessageA(
		FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		code,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPSTR) &lpMsgBuf,
		0,
		NULL
		);
	if( lpMsgBuf == NULL ){
		return "";
	}
	std::string ret = (const char*)lpMsgBuf;
	LocalFree(lpMsgBuf);
	return ret;
}

std::wstring lix::CRTErrorNoCategory::getTextW( errno_t code)
{
	return _wcserror(code);
}

std::string lix::CRTErrorNoCategory::getText( errno_t code)
{
	return strerror(code);
}

